package com.threadDemo;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MyThreadTest {
    public static void main(String[] args) throws InterruptedException {
        ProducerThreadPoolExecutor producerThreadPoolExecutor = new ProducerThreadPoolExecutor(4, 4, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(4), new ProducerThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        ConsumerThreadPoolExecutor consumerThreadPoolExecutor = new ConsumerThreadPoolExecutor(4, 4, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(4), new ConsumerThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        BoundedBuffer bb = new BoundedBuffer();
        CountDownLatch productCount = new CountDownLatch(4);
        CountDownLatch consumerCount = new CountDownLatch(4);
        //生产者
        for (int i = 0; i < 4; i++) {
            producerThreadPoolExecutor.execute(new Producer(bb,productCount));
        }
        //消费者
        for (int i = 0; i < 4; i++) {
            consumerThreadPoolExecutor.execute(new Consumer(bb,consumerCount));
        }
        productCount.await();
        consumerCount.await();
        producerThreadPoolExecutor.shutdown();
        consumerThreadPoolExecutor.shutdown();
        System.out.println("完成了操作");
    }
}
